import { reqLogin, reqLogOut } from '@/api/user'
export default {
  namespaced: true,
  state: {
    name: localStorage.getItem('name'),
    token: localStorage.getItem('token'),
  },
  actions: {
    // 登录
    async login({ commit }, { phone, password }) {
      const user = await reqLogin(phone, password)
      commit('LOGIN', user)
    },
    // 登出
    async logout({ commit }) {
      await reqLogOut()
      commit('LOGOUT')
    },
  },
  mutations: {
    // 登录存token
    LOGIN(state, user) {
      localStorage.setItem('name', user.name)
      localStorage.setItem('token', user.token)

      state.name = user.name
      state.token = user.token
    },
    // 登出注销token
    LOGOUT(state) {
      localStorage.removeItem('name')
      localStorage.removeItem('token')
      state.name = ''
      state.token = ''
    },
  },
}
